package com.ayuku.retail.ui.customers;

import java.math.BigDecimal;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;

import com.ayuku.commons.AyukuDB;
import com.ayuku.commons.FormDialogBasic;
import com.ayuku.commons.Util;
import com.ayuku.commons.UtilPojo;
import com.ayuku.retail.business.customers.CustomerImpl;
import com.ayuku.retail.persistence.model.Customer;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class FormCustomers extends FormDialogBasic {
	private CompCustomers cmp;
	private String custNo;
	private String alamat;
	private String nama;
	private Customer pjo;

	public FormCustomers(final Shell parent, final int style, final int mode) {
		super(parent, style, mode);
	}

	public void getPanelContent(final Group grpData) {
		cmp = new CompCustomers(grpData, SWT.NONE);
	}

	@Override
	public void additionalProses() {
		getDialogShell().setText("Add/Edit Customers");
		getDialogShell().setSize(388, 340);
		try {
			// Util.populateCmbGroup(cmpUser.cmbGroup);
		} catch (Exception e) {
			e.printStackTrace();
		}
		Util.setCenter(getDialogShell());
	}

	@Override
	public void clearContols() {
		cmp.txtNoPelanggan.setText("");
		cmp.txNama.setText("");
		cmp.txtAlamat.setText("");
		cmp.txtPhone.setText("");

		cmp.txtEmail.setText("");
		cmp.txtKontak.setText("");
		cmp.txtAlamat.setText("");
		cmp.txtTagihan.setValue(BigDecimal.valueOf(0.0));
	}

	@Override
	protected void doLoadAdd(String key) {
		// TODO Auto-generated method stub

	}

	@Override
	public void doLoad(String key) throws Exception {

		pjo = (Customer) AyukuDB.getPersist().readByPrimaryKey(
				Customer.class, new Object[] { key });

		cmp.txtNoPelanggan.setText(pjo.getCode());
		cmp.txNama.setText(pjo.getName());
		cmp.txtAlamat.setText(pjo.getAddress());		
		cmp.txtPhone.setText(pjo.getTelp());
		cmp.txtEmail.setText(pjo.getEmail());
		cmp.txtKontak.setText(pjo.getContactPerson());
		cmp.txtTagihan.setValue(pjo.getSaldoPiutang());
	}

	@Override
	public boolean doValidate() {
		custNo = cmp.txtNoPelanggan.getText();
		if (Util.isEmpty(custNo)) {
			Util.showMsgBox("Id harus dilengkapi!", 1,
					getDialogShell());
			cmp.txtNoPelanggan.setFocus();
			return false;
		}

		nama = cmp.txNama.getText();
		if (Util.isEmpty(nama)) {
			Util.showMsgBox("nama harus dilengkapi!", 1, getDialogShell());
			cmp.txNama.setFocus();
			return false;
		}

		alamat = cmp.txtAlamat.getText();
		if (Util.isEmpty(alamat)) {
			Util.showMsgBox("alamat harus dilengkapi!", 1, getDialogShell());
			cmp.txtAlamat.setFocus();
			return false;
		}

		return true;
	}

	@Override
	public void enabledControl(final boolean otherField, final boolean keyField) {
		cmp.txtNoPelanggan.setEditable(keyField);
		cmp.txNama.setEditable(otherField);
		cmp.txtAlamat.setEditable(otherField);
		cmp.txtPhone.setEditable(otherField);
		cmp.txtEmail.setEditable(otherField);
		cmp.txtKontak.setEditable(otherField);
		cmp.txtAlamat.setEditable(otherField);
		cmp.txtTagihan.setEnabled(otherField);
	}

	@Override
	public int doMainInsert() throws Exception {
		pjo = new Customer();
		setPojoWithData();
		return CustomerImpl.add(pjo);
	}

	@Override
	public int doMainDelete() throws Exception {
		return CustomerImpl.delete(pjo.getCode());
	}

	@Override
	public int doMainUpdate() throws Exception {
		setPojoWithData();
		return CustomerImpl.update(pjo);

	}

	@Override
	protected void setPojoWithData() throws Exception {
		pjo.setCode(custNo);
		pjo.setName(nama);
		pjo.setAddress(alamat);
		pjo.setTelp(Util.nullToString(cmp.txtPhone.getText()));
		pjo.setEmail(Util.nullToString(cmp.txtEmail.getText()));
		pjo.setContactPerson(Util.nullToString(cmp.txtKontak.getText()));
		pjo.setSaldoPiutang(cmp.txtTagihan.getValue());

		UtilPojo.setDateAndUser(pjo);
	}

	@Override
	protected void disabledOtherButton(boolean enable) {
		// TODO Auto-generated method stub

	}

}
